import axios from 'axios'

// 创建axios实例
const api = axios.create({
  baseURL: '/api',
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json',
  },
})

// 请求拦截器
api.interceptors.request.use(
  (config) => {
    // 可以在这里添加token等认证信息
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

// 响应拦截器
api.interceptors.response.use(
  (response) => {
    return response.data
  },
  (error) => {
    console.error('API请求错误:', error)
    return Promise.reject(error)
  }
)

// 交易相关API
export const transactionAPI = {
  // 获取所有交易记录
  getAll: (params?: any) => api.get('/transactions', { params }),
  
  // 获取单个交易记录
  getById: (id: number) => api.get(`/transactions/${id}`),
  
  // 创建交易记录
  create: (data: any) => api.post('/transactions', data),
  
  // 更新交易记录
  update: (id: number, data: any) => api.put(`/transactions/${id}`, data),
  
  // 删除交易记录
  delete: (id: number) => api.delete(`/transactions/${id}`),
}

// 分类相关API
export const categoryAPI = {
  // 获取所有分类
  getAll: (params?: any) => api.get('/categories', { params }),
  
  // 获取单个分类
  getById: (id: number) => api.get(`/categories/${id}`),
  
  // 创建分类
  create: (data: any) => api.post('/categories', data),
  
  // 更新分类
  update: (id: number, data: any) => api.put(`/categories/${id}`, data),
  
  // 删除分类
  delete: (id: number) => api.delete(`/categories/${id}`),
  
  // 初始化默认分类
  initialize: () => api.post('/categories/init'),
}

// 统计相关API
export const statisticsAPI = {
  // 获取统计概览
  getOverview: (params?: any) => api.get('/statistics/overview', { params }),
  
  // 获取分类统计
  getCategories: (params?: any) => api.get('/statistics/categories', { params }),
  
  // 获取月度统计
  getMonthly: (params?: any) => api.get('/statistics/monthly', { params }),
  
  // 获取账户统计
  getAccounts: (params?: any) => api.get('/statistics/accounts', { params }),
}

export default api